package com.pda.common.pages.config;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.database.DataBaseControl;
import com.pda.common.dialogs.ChooseDialog;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.tools.ToastUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConfigActivity extends BaseActivity<ConfigModel, ConfigView, ConfigPresent> implements ConfigView {
    private String TAG = "ConfigActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.ivu)
    ImageView ivu;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.ivp)
    ImageView ivp;
    @BindView(R.id.login_p)
    EditText loginP;
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.baseinfbt)
    Button baseinfbt;
    @BindView(R.id.clearbt)
    Button clearbt;
    String cid = "";
    String add = "";
    ChooseDialog nDialog;

    @Override
    protected void initdata() {
        tvTitle.setText("网络配置");
        loginId.setText(SharedPreferencesManger.getCid());
        loginP.setText(SharedPreferencesManger.getHostName());
//        loginId.setText("3079");
//        loginP.setText("saas.zaosin.cn");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    public ConfigModel createModel() {
        return new ConfigModelImpl(this);
    }

    @Override
    public ConfigView createView() {
        return this;
    }

    @Override
    public ConfigPresent createPresenter() {
        return new ConfigPresent();
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvr.setText(str);
            }
        });
    }

    @Override
    public void tips(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvr.append("\n" + str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt, R.id.baseinfbt,R.id.clearbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.loginbt:
                cid = loginId.getText().toString();
                if (cid.length() == 0) {
                    showMsg("请填写企业编号");
                    return;
                }
                add = loginP.getText().toString();
                if (add.length() == 0) {
                    showMsg("请填写服务器地址");
                    return;
                }
                presenter.config(add, cid);
                break;
            case R.id.baseinfbt:
                cid = SharedPreferencesManger.getCid();
                add = SharedPreferencesManger.getHostName();
                if (cid.length() == 0 || add.length() == 0) {
                    ToastUtils.showShort(this, "下载前请先完成网络配置");
                    return;
                }
                tvr.setText("");
                DataBaseControl.deleteProductAll();
                DataBaseControl.deleteDealerAll();
                presenter.download();
                break;
            case R.id.clearbt:
                nDialog = new ChooseDialog(this, "是否清除条码", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        DataBaseControl.deleteCodeAll();
                        ToastUtils.showShort(ConfigActivity.this, "已清除全部条码");
                    }
                });
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.setCanceledOnTouchOutside(false);
                nDialog.show();
                break;
        }
    }
}
